//
//  BDReservationTableViewCell.m
//  BadBoy
//
//  Created by wen on 2017/7/6.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDReservationTableViewCell.h"
#import "BDReservationModel.h"
#import "BDOrderModel.h"

@interface BDReservationTableViewCell ()

@property (nonatomic, strong) UILabel *currentPriceLabel;
@property (nonatomic, strong) UILabel *oldPriceLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *titleImageView;
@property (nonatomic, strong) UIImageView *stateImageView;
@property (nonatomic, strong) UIView *topLine;
@property (nonatomic, strong) UIView *bottomLine;

@property (nonatomic, copy) void(^buttonClickBlock)();

@end

@implementation BDReservationTableViewCell

- (void)configeWithModel:(id)model buttonClickBlock:(void(^)())buttonClickBlock{
    self.buttonClickBlock = buttonClickBlock;
    
    if ([model isKindOfClass:[BDReservationModel class]]) {
        BDReservationModel *reservationModel = model;
        
        self.titleLabel.text = reservationModel.shop.title;
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:reservationModel.coupon.cover] placeholderImage:[UIImage imageNamed:@"shop_moren.jpg"]];
        self.textLabel.text = reservationModel.coupon.title;
        self.currentPriceLabel.text = [NSString stringWithFormat:ASLocalizedString(@"实付： ¥%@"), [NSDecimalNumber decimalNumberWithString:reservationModel.receipt_amount]];
        self.oldPriceLabel.text = [NSString stringWithFormat:ASLocalizedString(@"原价： ¥%@"), [NSDecimalNumber decimalNumberWithString:reservationModel.total_amount]];
        self.timeLabel.text = [NSString stringWithFormat:ASLocalizedString(@"有效期：%@ 至 %@"), reservationModel.coupon.startTimeString, reservationModel.coupon.endTimeString];
        
        //未使用
        if (reservationModel.status == 0) {
            self.button.backgroundColor = KBDColorBlue;
            [self.button setTitle:ASLocalizedString(@"使用")forState:UIControlStateNormal];
            [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.stateImageView.hidden = YES;
            self.button.hidden = NO;
            self.button.userInteractionEnabled = YES;
        }else if (reservationModel.status == 1) {
            self.button.hidden = NO;
            self.stateImageView.hidden = NO;
            self.stateImageView.image = [UIImage imageNamed:@"shop_shiyong"];
            self.button.backgroundColor = KBDColorBlue;
            [self.button setTitle:ASLocalizedString(@"评价")forState:UIControlStateNormal];
            [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.button.userInteractionEnabled = YES;
        }else if (reservationModel.status == 2) {
            self.button.hidden = YES;
            self.stateImageView.hidden = NO;
            self.stateImageView.image = [UIImage imageNamed:@"shop_shixiao"];
        }else if (reservationModel.status == 3) {
            self.button.hidden = NO;
            self.stateImageView.hidden = YES;
            self.button.backgroundColor = [UIColor whiteColor];
            [self.button setTitle:ASLocalizedString(@"退款中")forState:UIControlStateNormal];
            [self.button setTitleColor:[UIColor colorWithHexString:@"#767676"] forState:UIControlStateNormal];
            self.button.userInteractionEnabled = NO;
        }else if (reservationModel.status == 4) {
            self.button.hidden = NO;
            self.stateImageView.hidden = NO;
            self.stateImageView.image = [UIImage imageNamed:@"shop_shixiao"];
            self.button.backgroundColor = [UIColor whiteColor];
            [self.button setTitle:ASLocalizedString(@"已退款")forState:UIControlStateNormal];
            [self.button setTitleColor:[UIColor colorWithHexString:@"#767676"] forState:UIControlStateNormal];
            self.button.userInteractionEnabled = NO;
        }else if (reservationModel.status == 5) {
            self.button.hidden = YES;
            self.stateImageView.hidden = NO;
            self.stateImageView.image = [UIImage imageNamed:@"shop_shiyong"];
        }
    }else if ([model isKindOfClass:[BDOrderModel class]]) {
        BDOrderModel *order = model;
        
        self.titleLabel.text = order.shop_title;
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:order.coupon.cover] placeholderImage:[UIImage imageNamed:@"shop_moren.jpg"]];
        self.textLabel.text = order.coupon.title;
        self.currentPriceLabel.text = [NSString stringWithFormat:@"¥%@", [NSDecimalNumber decimalNumberWithString:order.coupon.discount_price]];
        self.oldPriceLabel.text = [NSString stringWithFormat:ASLocalizedString(@"原价： ¥%@"), [NSDecimalNumber decimalNumberWithString:order.coupon.price]];
        self.timeLabel.text = [NSString stringWithFormat:ASLocalizedString(@"有效期：%@ 至 %@"), order.coupon.startTimeString, order.coupon.endTimeString];
        
        self.button.userInteractionEnabled = YES;
        self.button.backgroundColor = KBDColorBlue;
        [self.button setTitle:ASLocalizedString(@"支付")forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.stateImageView.hidden = YES;
        self.button.hidden = NO;
    }
}

- (void)layoutSubviews{
    self.titleImageView.size = CGSizeMake(15, 15);
    self.titleImageView.left = 10;
    self.titleImageView.centerY = 35 / 2.0;
    
    [self.titleLabel sizeToFit];
    self.titleLabel.left = self.titleImageView.right + 6;
    self.titleLabel.centerY = self.titleImageView.centerY;
    
    self.topLine.bottom = 35;
    
    self.imageView.size = CGSizeMake(75, 65);
    self.imageView.left = 10;
    self.imageView.top = self.topLine.bottom + 12;
    
    self.textLabel.font = [UIFont systemFontOfSize:15];
    self.textLabel.textColor = [UIColor colorWithHexString:@"#101010"];
    [self.textLabel sizeToFit];
    self.textLabel.left = self.imageView.right + 10;
    self.textLabel.top = self.imageView.top + 3;
    
    self.currentPriceLabel.font = [UIFont systemFontOfSize:16];
    self.currentPriceLabel.textColor = [UIColor colorWithHexString:@"#ff4f00"];
    [self.currentPriceLabel sizeToFit];
    self.currentPriceLabel.left = self.textLabel.left;
    self.currentPriceLabel.bottom = self.imageView.bottom - 3;
    
    self.oldPriceLabel.font = [UIFont systemFontOfSize:13];
    self.oldPriceLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    [self.oldPriceLabel sizeToFit];
    self.oldPriceLabel.left = self.currentPriceLabel.right + 15;
    self.oldPriceLabel.bottom = self.currentPriceLabel.bottom;
    
    self.button.size = CGSizeMake(65, 30);
    self.button.layer.masksToBounds = YES;
    self.button.layer.cornerRadius = 3;
    self.button.right = kBDScreenWidth - 10;
    self.button.centerY = self.imageView.centerY;
    self.button.titleLabel.font = [UIFont systemFontOfSize:13];
    
    self.bottomLine.top = self.imageView.bottom + 12;
    
    self.timeLabel.font = [UIFont systemFontOfSize:12];
    self.timeLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    [self.timeLabel sizeToFit];
    self.timeLabel.left = 10;
    self.timeLabel.centerY = self.height - 17.5;
    
    self.stateImageView.size = CGSizeMake(60*1.3, 45.5*1.3);
    self.stateImageView.right = self.width - 10;
    self.stateImageView.top = 0;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        self.timeLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_timeLabel];
    }
    return _timeLabel;
}

- (UILabel *)currentPriceLabel{
    if (!_currentPriceLabel) {
        self.currentPriceLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_currentPriceLabel];
    }
    return _currentPriceLabel;
}

- (UILabel *)oldPriceLabel{
    if (!_oldPriceLabel) {
        self.oldPriceLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_oldPriceLabel];
    }
    return _oldPriceLabel;
}

- (UIButton *)button{
    if (!_button) {
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_button];
        [[_button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (self.buttonClickBlock) {
                self.buttonClickBlock();
            }
        }];
    }
    return _button;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        self.titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#111111"];
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UIImageView *)titleImageView{
    if (!_titleImageView) {
        self.titleImageView = [[UIImageView alloc] init];
        _titleImageView.image = [UIImage imageNamed:@"dianpu"];
        [self.contentView addSubview:_titleImageView];
    }
    return _titleImageView;
}

- (UIImageView *)stateImageView{
    if (!_stateImageView) {
        self.stateImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_stateImageView];
    }
    return _stateImageView;
}

- (UIView *)topLine{
    if (!_topLine) {
        self.topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 1)];
        _topLine.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
        [self.contentView addSubview:_topLine];
    }
    return _topLine;
}

- (UIView *)bottomLine{
    if (!_bottomLine) {
        self.bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 1)];
        _bottomLine.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
        [self.contentView addSubview:_bottomLine];
    }
    return _bottomLine;
}
@end
